package com.matrix.yukun.matrix.util;


import android.os.CountDownTimer;

/**倒计时的工具
 */
public class OperationCountdownTimer extends CountDownTimer {
    protected Callback callback;
    /**
     * @param millisInFuture    需要倒计时的总时间，单位毫秒
     * @param countDownInterval 倒计时的时间间隔，单位毫秒
     * @param callback 倒计时回调
     */
    public OperationCountdownTimer(long millisInFuture, long countDownInterval, Callback callback) {
        super(millisInFuture, countDownInterval);
        this.callback = callback;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        if(callback != null) {
            callback.onTick(millisUntilFinished);
        }
    }

    @Override
    public void onFinish() {
        if(callback != null) {
            callback.onCountdownFinish();
        }
    }

    /**
     * 释放资源
     */
    public void release() {
        cancel();
        callback = null;
    }
    /**
     * 倒计时的回调
     */
    public interface Callback {
        /**每次改变倒计时回调
         * @param millisUntilFinished
         */
        void onTick(long millisUntilFinished);

        /**
         * 当倒计时完成时回调
         */
        void onCountdownFinish();
    }
}
